//
//  JHMiddleListTopDetailController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/16.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHMiddleListTopDetailController.h"
#import <WebKit/WebKit.h>
#import "CNScollPositionView.h"
#import "JHSettingFuntTopView.h"
#import "JHShareBottomView.h"

@interface JHMiddleListTopDetailController ()<WKUIDelegate,WKNavigationDelegate,UIScrollViewDelegate>
@property(nonatomic)CNScollPositionView  *positionView;
@property(nonatomic)UIScrollView *scroView;
@property (nonatomic,strong) WKWebView *webView1;
@property (nonatomic,strong) WKWebView *webView2;
@property (nonatomic,assign) NSInteger webFont;
@property (nonatomic,copy) NSString *url;
@property (nonatomic,strong) NSString *htmlString;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) JHSettingFuntTopView *popFuntView;
@property (nonatomic,strong) UIView *coverView;
@end

@implementation JHMiddleListTopDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=self.model.unitname;
    self.webFont=2;  /// 1：小号字体，2：中号字体，3：大号字体
    self.dataArray=[NSArray array];
    int founTag;
    NSString *fount=[kUserDefaults objectForKey:@"retFount"];
    if (fount) {
        self.webFont=[fount intValue];
    }else{
        self.webFont=2;  /// 0：小号字体，1：中号字体，2：大号字体  3:超大
    }
    
    
    [self initUI];
    [self initCoverView];
    [self setUpNav];
 
}

- (void)setUpNav
{
    UIButton*rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"字体设置" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(settingFuntBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}

- (void)initCoverView
{
    //蒙版的View
    self.coverView=[[UIView alloc]init];
    self.coverView.frame=CGRectMake(0, 0,JKScreenW, JKScreenH);
    self.coverView.backgroundColor=[UIColor blackColor];
    self.coverView.alpha=0.2;
    self.coverView.hidden=YES;
    [kAppWindow addSubview:self.coverView];
    
}


- (void)settingFuntBtnDidClick:(UIButton *)button
{
    WEAKSELF
    JHSettingFuntTopView *funtView=[[[NSBundle mainBundle]loadNibNamed:@"JHSettingFuntTopView" owner:self options:nil]lastObject];
    funtView.frame=CGRectMake(0, 0, JKScreenW, 130);
    self.popFuntView=funtView;
    self.coverView.hidden=NO;
    funtView.center=CGPointMake(JKScreenW/2, (JKScreenH-kTopHeight)/2);
    [kAppWindow addSubview:funtView];
    
    [self.popFuntView setSureBlock:^{
        weakSelf.coverView.hidden=YES;
        weakSelf.popFuntView.hidden=YES;
        NSString *fount=[kUserDefaults objectForKey:@"retFount"];
        if (fount) {
            weakSelf.webFont=[fount intValue];
        }
        [weakSelf initUI];
    }];
}

- (void)setUpBottom
{
    UIView *bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, JKScreenH-kTabBarHeight-kTopHeight-10, JKScreenW, 60)];
    bottomView.backgroundColor=[UIColor whiteColor];
    UIButton *shareBtn=[[UIButton alloc]initWithFrame:CGRectMake(30, 10,JKScreenW-60, 40)];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    shareBtn.backgroundColor=JKMainColor;
    shareBtn.layer.cornerRadius=6.0;
    shareBtn.titleLabel.textColor=[UIColor whiteColor];
    [shareBtn addTarget:self action:@selector(shareBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:shareBtn];
    [self.view addSubview:bottomView];
}

#pragma mark-- - 分享
- (void)shareBtnDidClick:(UIButton *)button
{
    DXShareView *shareView = [[DXShareView alloc] init];
    DXShareModel *shareModel = [[DXShareModel alloc] init];
    [shareView showShareViewWithDXShareModel:shareModel shareContentType:DXShareContentTypeLink];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    WEAKSELF
    weakSelf.coverView.hidden=YES;
    weakSelf.popFuntView.hidden=YES;
    NSString *fount=[kUserDefaults objectForKey:@"retFount"];
    if (fount) {
        weakSelf.webFont=[fount intValue];
    }
    [weakSelf initUI];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self initData];
}


- (void)initData
{
    // 把txt替换为HTML
    self.url= [self.model.data.textfile stringByReplacingOccurrencesOfString:@".txt"withString:@""];
    NSString *path = [[NSBundle mainBundle] pathForResource:self.url ofType:@"txt"];
    NSError *error = nil;
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    self.htmlString=[html stringByReplacingOccurrencesOfString:self.model.data.titlename withString:@""];
    NSLog(@"%@-------获取到的字符串---",self.htmlString);
    self.dataArray=[self.htmlString componentsSeparatedByString:@"*"];
    [self setUpUI];
}

- (void)setUpUI
{
    self.automaticallyAdjustsScrollViewInsets=NO;
    CNScollPositionView  *postionView = [[CNScollPositionView alloc]init];
    postionView.frame=CGRectMake(0, 0,JKScreenW,40);
    postionView.titlesArr = [self.model.data.titlename componentsSeparatedByString:@"_"];
    postionView.contentScrollView = self.scroView;
    postionView.backgroundColor =JKMainColor;
    [self.view addSubview:postionView];
    self.positionView = postionView;
    [self.view addSubview:self.scroView];
    [self addScroviewSubViews];
}


-(void)addScroviewSubViews{
    for (int i = 0; i<self.positionView.titlesArr.count; i++) {
        WKWebView *webView = [[WKWebView alloc] init];
        webView.frame =CGRectMake(0,0,JKScreenW, JKScreenH-kTopHeight);
        self.webView2=webView;
        webView.UIDelegate=self;
        webView.navigationDelegate=self;
        [self.scroView addSubview:webView];
        NSString *htmlUrl=self.dataArray[i];
        NSLog(@"%@--------------jjjaajaj",htmlUrl);
        //[self setUpBottom];
        [self.webView2 loadHTMLString:htmlUrl baseURL:nil];
        
    }
}
-(UIScrollView *)scroView{
    if (!_scroView ) {
        _scroView = [[UIScrollView alloc]init];
        _scroView.bounces = NO;
        _scroView.delegate =self;
        _scroView.pagingEnabled=YES;
        //        [_scroView.panGestureRecognizer addTarget:self action:@selector(pan:)];
    }
    return _scroView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger   pageIndex = scrollView.contentOffset.x/self.scroView.bounds.size.width;
    [self.positionView resetTitileViewState:pageIndex];
    self.positionView.titleContentView.scroll=NO;
    NSLog(@"%ld-------滑动的页数---",pageIndex);
}
-(void)viewWillLayoutSubviews{
    if (@available(iOS 11.0, *)) {
        self.positionView.frame = CGRectMake(0,self.view.safeAreaInsets.top, self.view.bounds.size.width, 40);
    } else {
        self.positionView.frame = CGRectMake(0,64, self.view.bounds.size.width, 40);
    }
    self.scroView.frame = CGRectMake(0, self.positionView.frame.origin.y+self.positionView.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height-self.positionView.bounds.size.height-self.positionView.frame.origin.y);
    self.scroView.contentSize = CGSizeMake(self.view.bounds.size.width*self.positionView.titlesArr.count, 0);
    for (int  i = 0; i<self.scroView.subviews.count; i++) {
        UILabel *lab =self.scroView.subviews[i];
        lab.frame = CGRectMake(self.scroView.bounds.size.width*i, 0,self.scroView.bounds.size.width, self.scroView.bounds.size.height);
    }
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    //禁止用户选择
    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
    
    if (self.webFont==1) {
        // 适当增大字体大小
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
    }else if(self.webFont==2){
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'" completionHandler:nil];
    }else{
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'" completionHandler:nil];
    }

}

@end

